package ModelLayer;
import java.util.ArrayList;

/**
 * A ArrayList container used for storing objects of the class DVD
 */
public class DVDContainer
{   
    private ArrayList<DVD> dvdContainer;
    private static DVDContainer instance;
    int index;
    
    /**
     * Constructor for objects of class DVDContainer
     */
    public DVDContainer()
    {
        dvdContainer = new ArrayList<DVD>();
        index = 0;
    }
    
    public static DVDContainer getInstance()
    {
        if(instance == null)
        {
            instance = new DVDContainer();
        }
        return instance;
    }
        
    public void addDVD(String title, long id, String artist,  String publicationDate)
    {   
        DVD newDVD = new DVD(id, title, artist, publicationDate);
        dvdContainer.add(newDVD);
    }
    
    public boolean findDVD(String search)
    {
        int index = 0;
        boolean found = false;
            while(index < dvdContainer.size() && !found)
            {
                DVD searchTitle = dvdContainer.get(index);
                    if(searchTitle.getTitle().equalsIgnoreCase(search))
                    {
                        return found = true;                     
                    }
                    else
                    {
                        index++;
                    }
                        
            }
        return found = false;    

    }
    
    public DVD getDVD(String search)
    {    
        
        findDVD(search);
        return dvdContainer.get(index);

    }
    
    public void updateDVD(String search, String title, long id, String artist, String publicationDate)
    {
        findDVD(search);
        dvdContainer.get(index).setTitle(title);
        dvdContainer.get(index).setId(id);
        dvdContainer.get(index).setArtist(artist);
        dvdContainer.get(index).setPublicationDate(publicationDate);
    } 

    public void deleteDVD(String title)
    {
        findDVD(title);
        dvdContainer.remove(index);        
    }
    
    public void addCopy(String title, String serialNumber, String purchaseDate, String purchasePrice)
    {   
        if (findDVD(title) != false)
        {
            Copy newCopy = new Copy(serialNumber, purchaseDate, purchasePrice);
            dvdContainer.get(index).copyList.add(newCopy);
        }
    }
   
    public Copy findCopy(String title)
    {   
        findDVD(title);  
        int i = index;
        int j = dvdContainer.get(i).copyList.size();
        return dvdContainer.get(i).copyList.get(0);
    } 
    
    public void deleteCopy(String title, String serialNumber)
    {   
         for (int i = 0; i < dvdContainer.size(); i++)
        {   
            for (int j = 0; 
                 j < dvdContainer.get(i).copyList.size();
                 j++)
                 {
                    if (dvdContainer.get(i).copyList.get(j).getSerialNumber().equals(serialNumber))
                    {
                        dvdContainer.get(i).copyList.remove(j);
                    }                     
                 }
        }
    }
    
    public ArrayList listAllDVDs()
    {
        ArrayList<String> completeDVDList = new ArrayList<String>(); 
        for(DVD dvd : dvdContainer)
        {
            completeDVDList.add(" Title: " + dvd.getTitle() + "." + 
                                " ID: " + Long.toString(dvd.getId()) + "." +
                                " Artist: " + dvd.getArtist() + "." + 
                                " Publication Date: " + dvd.getPublicationDate() + "." +
                                System.lineSeparator());
        }
        return completeDVDList;
    } 

}
